<template>
  <div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
  :immediate-check = false
>
<Articleltem v-for="obj in articleList " :key="obj.art_id" :artObj="obj" @click="artDetailFn(obj.art_id)"></Articleltem>
</van-list>
</van-pull-refresh>

  </div>
</template>

<script>
import Articleltem from '@/components/Articleltem.vue'
import { getArticleListAPI } from '@/api'
export default {
  data () {
    return {
      articleList: [],
      nextTime: new Date(),
      loading: false, // 底部刷新状态
      refreshing: false, // 顶部刷新状态
      finished: false
    }
  },
  props: {
    channelId: Number
  },
  components: {
    Articleltem
  },
  created () {
    // console.log(this.channelId)

    // const res = await getArticleListAPI({
    //   channel_id: this.channelId,
    //   timestamp: this.nextTime
    // })
    // console.log(res)
    //   this.articleList = res.data.data.results
    //   this.nextTime = res.data.data.pre_timestamp
    //   this.loading = false
    // },
    this.getArticleListFn()
  },
  methods: {
    async onLoad () {
      // if (this.articleList.length > 0) {
      //   // 异步更新数据
      //   const res = await getArticleListAPI({
      //     channel_id: this.channelId,
      //     timestamp: this.nextTime
      //   })
      //   // console.log(res)
      //   // if (res.data.data && res.data.data.results && res.data.data.pre_timestamp === null) {
      //   if (res.data?.data?.pre_timestamp === null) {
      //     this.finished = true
      //     this.nextTime = null
      //   } else {
      //     this.nextTime = res.data.data.pre_timestamp
      //   }
      //   // console.log(res)
      //   this.articleList = [...this.articleList, ...res.data.data.results]
      //   this.nextTime = res.data.data.pre_timestamp
      //   this.loading = false
      // }
      if (this.articleList.length > 0) {
        this.getArticleListFn()
      }
    },
    onRefresh () {
      // 清空列表数据
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.articleList = []
      this.nextTime = new Date()
      this.getArticleListFn()
    },

    // 获取文章列表
    async  getArticleListFn () {
      const res = await getArticleListAPI({
        channel_id: this.channelId,
        timestamp: this.nextTime
      })
      if (res.data?.data?.pre_timestamp === null) {
        this.finished = true
        this.nextTime = null
      }
      this.articleList = [...this.articleList, ...res.data.data.results]
      this.nextTime = res.data.data.pre_timestamp
      this.loading = false
      setTimeout(() => {
        this.refreshing = false
      }, 1000)
    },
    artDetailFn (artId) {
      console.log(123)
      this.$router.push({
        path: '/art_detall',
        query: {
          aid: artId
        }
      })
    }
  }
}
</script>

<style>

</style>
